<template>
  <div class="finance">
    <el-form
      ref="settingForm"
      class="setting-form"
      :rules="formDataRules"
      :model="formData"
      label-position="left"
      label-width="160px"
    >
      <el-form-item label="余额发红包" prop="balance_send">
        <el-switch
          v-model="formData.balance_send"
          active-color="#13ce66"
        />
      </el-form-item>
      <el-form-item label="最低提现金额" prop="low_deposit">
        <el-input v-model="formData.low_deposit" placeholder="最低提现金额" />
      </el-form-item>
      <el-form-item label="提现审核金额" prop="deposit_check">
        <el-input v-model="formData.deposit_check" placeholder="提现金额大于多少需要审核(0=全部提现都需要审核)" />
      </el-form-item>
      <el-form-item label="提现成功备注信息" prop="remark">
        <el-input v-model="formData.remark" placeholder="提现成功备注信息" />
      </el-form-item>
      <el-button type="primary" size="small" @click="submit">确认配置</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Finance',
  props: {
    setting: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {
        balance_send: false, // 是否余额发红包
        low_deposit: 0.3 // 最低提现金额
      },
      formDataRules: {
        low_deposit: [
          { required: true, message: '请输入正确的金额', trigger: 'blur' },
          { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额', trigger: 'blur' },
          { validator: function(rule, value, callback) {
            if (parseFloat(value) < 0.3) {
              callback('最低金额不能小于0.3元')
            } else {
              callback()
            }
          }
          }
        ],
        deposit_check: [
          { required: true, message: '请输入正确的金额', trigger: 'blur' },
          { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '提现备注信息不能为空', trigger: 'blur' },
          { max: 20, message: '备注信息长度不能大于20个字', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    setting() {
      const { balance_send, low_deposit, deposit_check, remark } = this.setting
      this.formData = {
        balance_send: !!balance_send,
        low_deposit,
        deposit_check,
        remark
      }
    }
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.settingForm.validate((valid) => {
        if (valid) {
          const {
            balance_send,
            low_deposit,
            deposit_check,
            remark
          } = this.formData
          this.$put('setting/finance', {
            balance_send: balance_send ? 1 : 0,
            low_deposit,
            deposit_check,
            remark
          })
            .then(() => {
              this.$message.success({
                message: '修改成功',
                type: 'success'
              })
            })
            .catch((error) => {
              this.$handleError(error)
            })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .setting-form{
    width: 600px;
  }
</style>
